<template>
  <div>
    <div ref="rank_ref" class="com-chart" />
  </div>
</template>

<script>
import rank from "@/assets/js/rank.json"
export default {
  data() {
    return {
      chartInstane: null,
      allData: null, // 服务器的数据
      startValue: 0, // 区域缩放的起点值
      endValue: 9, // 区域缩放的终点值
      timerId: null
    }
  },
  mounted() {
    this.initChart() // 初始化
    this.getData()
    window.addEventListener("resize", this.screenAdapter)
    // 初次加载，调用一次
    this.screenAdapter()
  },
  // 取消监听
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter)
    clearInterval(this.timerId)
  },
  methods: {
    initChart() {
      this.chartInstane = this.$echarts.init(this.$refs.rank_ref, "chalk")
      const initOption = {
        grid: {
          top: "10%",
          right: "5%",
          left: "5%",
          bottom: "5%",
          containLabel: true
        },
        tooltip: {
          show: true
        },
        xAxis: {
          type: "category" // 类目型
        },
        yAxis: {
          type: "value" // 数值型
        },
        series: [
          {
            type: "bar" // 柱状图配置
          }
        ]
      }
      this.chartInstane.setOption(initOption)
      this.chartInstane.on("mouseover", () => {
        clearInterval(this.timerId)
      })
      this.chartInstane.on("mouseout", () => {
        this.startInterVal()
      })
    },
    // 获取服务器数据
    getData() {
      // const { data: res } = await this.$http.get('/api/rank')
      this.allData = rank
      // console.log(this.allData)
      // 对数据进行排序  从大到小
      this.allData.sort((a, b) => {
        return b.value - a.value
      })
      this.updateChart()
      // 启动定时器
      this.startInterVal()
    },
    // 更新图表数据
    updateChart() {
      const colorArr = [
        ["#0BA82C", "#4FF778"],
        ["#2E72BF", "#23E5E5"],
        ["#5052EE", "#AB6EE5"]
      ]
      // 更新数据
      // 所有省份的数组
      const provinceArr = this.allData.map(item => {
        return item.name
      })
      // 所有省份对应的销售金额
      const valueArr = this.allData.map(item => {
        return item.value
      })
      const dataOption = {
        xAxis: {
          data: provinceArr
        },
        dataZoom: {
          // 区域缩放
          show: false,
          startValue: this.startValue,
          endValue: this.endValue
        },
        series: {
          data: valueArr,
          itemStyle: {
            color: arg => {
              let tarfetColorArr = null
              if (arg.value > 300) {
                tarfetColorArr = colorArr[0]
              } else if (arg.value > 200) {
                tarfetColorArr = colorArr[1]
              } else {
                tarfetColorArr = colorArr[2]
              }
              return new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
                {
                  offset: 0,
                  color: tarfetColorArr[0]
                },
                {
                  offset: 1,
                  color: tarfetColorArr[1]
                }
              ])
            }
          }
        }
      }
      this.chartInstane.setOption(dataOption)
    },
    // 屏幕适配
    screenAdapter() {
      const tilteFontSize = (this.$refs.rank_ref.offsetWidth / 100) * 3.6
      const adapterOption = {
        series: [
          {
            barWidth: tilteFontSize,
            itemStyle: {
              barBorderRadius: [tilteFontSize / 2, tilteFontSize / 2, 0, 0]
            }
          }
        ]
      }
      this.chartInstane.setOption(adapterOption)
      // 图表自适应调用
      this.chartInstane.resize()
    },
    // 设置定时器
    startInterVal() {
      if (this.timerId) {
        clearInterval(this.timerId)
      }
      this.timerId = setInterval(() => {
        this.startValue++
        this.endValue++
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0
          this.endValue = 9
        }
        this.updateChart()
      }, 5000)
    }
  }
}
</script>
<style lang="scss" scoped>
.com-chart {
  height: 260px;
  width: 100%;
}
</style>
